﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 字典定义
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BarCordCreateController : ControllerBase
    {
        private readonly ILogger<WMSbarCordCreateRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public BarCordCreateController(ILogger<WMSbarCordCreateRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");

        }

        /// <summary>
        /// WMS供应商条码条码创建
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostBarCordCreate([FromBody] WMSbarCordCreateRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WMSbarCordCreateRequestDto result = new WMSbarCordCreateRequestDto();
            WMSbarCordCreateResponseDto re = new WMSbarCordCreateResponseDto();
            string token_no = strparm.token_no;
            string supplier_code = strparm.supplier_code;
            string po_no = strparm.po_no;
            string create_by = strparm.create_by;
            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);

            try
            {
                strSql = new StringBuilder();
                strSql.Append("select * from pur_purchase_order where purchase_no='" + po_no + "' LIMIT 1");
                DataTable dt_pocheck = DB.Query(strSql.ToString(), con);
                if (dt_pocheck.Rows.Count<0)
                {
                    strMsg = "WMS系统不存在《"+ po_no+"》采购订单号，请检查清楚再调用接口";
                }


                strSql = new StringBuilder();
                strSql.Append("select * from GK_Supplier_Token where supplier_code='" + supplier_code + "' and token_no='" + token_no + "' LIMIT 1");
                DataTable dt_token = DB.Query(strSql.ToString(), con);

                if (dt_token.Rows.Count > 0&& strMsg=="")
                {

                    string supplier_name = dt_token.Rows[0]["supplier_name"].ToString();
                    var row = strparm.rows;
                    string ddate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    foreach (var item in row)
                    {

                        string parent_barcode_no = item.parent_barcode_no;
                        string ancestors = item.ancestors;
                        string barcode_type = item.barcode_type;
                        string barcode_no = item.barcode_no;
                        string barcode_status = item.barcode_status;
                        string rule_code = item.rule_code;
                        string source_batch = item.source_batch;
                        string material_code = item.material_code;
                        string material_amount = item.material_amount;
                        string material_unit = item.material_unit;





                        strSql = new StringBuilder();
                        strSql.Append("select * from GK_Supplier_Material where supplier_code='" + supplier_code + "' and supplier_materialcode='" + material_code + "' LIMIT 1");
                        DataTable dt_material = DB.Query(strSql.ToString(), con);
                        if (dt_material.Rows.Count>0&& strMsg=="")
                        {
                            string gk_materialcode = dt_material.Rows[0]["gk_materialcode"].ToString();
                            string gk_materialname = dt_material.Rows[0]["gk_materialname"].ToString();
                            strSql = new StringBuilder();
                            strSql.Append(@"insert into barcode_file (dept_id,parent_barcode_no,ancestors,barcode_type,barcode_no,barcode_status
,barcode_identification,rule_code,source_no,source_batch,supplier_code,supplier_name,priority
,material_code,material_name,material_amount,material_unit,remark,create_by,create_time)  ");
                            strSql.Append(@" VALUES (");
                            strSql.Append(@" '100','" + parent_barcode_no + "','" + ancestors + "'," + barcode_type + ",'" + barcode_no + "','" + barcode_status + "','0','barcode_no','" + po_no + "','" + source_batch + "','" + supplier_code + "','" + supplier_name + "'," +
                                "'20','" + gk_materialcode + "','" + gk_materialname + "','" + material_amount + "','" + material_unit + "','供应商条码推送','GKIT01','" + ddate + "'");
                            strSql.Append(@" )");
                            strSqlList.Add(strSql.ToString());
                        }
                        else
                        {
                            strMsg = "供应商物料对照表，不存在编码《"+ material_code + "》的对照记录，请联系管理员在表GK_Supplier_Material内添加";
                        }
                        
                    }
                    if (strSqlList.Count > 0&& strMsg=="")
                    {
                        bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                        if (A = false)
                        {
                            strMsg = "生成条码失败，数据库执行失败";
                        }
                    }

                }
                else
                {
                    if (strMsg=="")
                    {
                        strMsg = "供应商编码或TOKEN密码错误，请检查清楚再调用接口";
                    }
                    else
                    {
                        strMsg = DB.strmsg.ToString();
                    }
                   
                }

            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "1";
            }
            else
            {
                re.status = "失败";
                re.code = "0";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
